/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wwcontrolserver.ifk;
import wwcontrolserver.datarecorders.AbstractDataRecorder;
import wwcontrolserver.datarecorders.WWControlServerLogger;
import com.phidgets.InterfaceKitPhidget;
import com.phidgets.PhidgetException;
import com.phidgets.event.AttachListener;
import com.phidgets.event.DetachListener;
import com.phidgets.event.InputChangeListener;
import com.phidgets.event.OutputChangeListener;
import com.phidgets.event.SensorChangeListener;
//import java.io.BufferedWriter;
import java.util.ArrayList;
//import org.tmatesoft.sqljet.core.SqlJetException;
/**
 *
 * @author Michael Ferry
 */
public abstract class AbstractIFK {
    public static boolean ANALOGINPUT;
    public static boolean DIGITALINPUT;
    public static boolean DIGITALOUTPUT;

    protected final String ifkName;
    //protected WWControlServerLogger WWCSlog;
    //private SensorContainer SContainer;
    protected InterfaceKitPhidget ifk;
    protected final int ifkSerial;

    public AbstractIFK(int ifkSerial, String ifkName,
            AttachListener AL, DetachListener DL) throws PhidgetException {

        //this.WWCSlog = WWCSlog;
        //this.SContainer = SContainer;
        this.ifkSerial = ifkSerial; 
        this.ifkName = ifkName;
        this.ifk = new InterfaceKitPhidget();
        this.ifk.addAttachListener(AL);
        this.ifk.addDetachListener(DL);

    }
    public String GetIfkName(){
        return ifkName;
    }
    public int GetIfkSerial(){
        return ifkSerial;
    }
    public void open() throws PhidgetException{    
        ifk.open(ifkSerial);   
    }
    //Shutsdown all DO if they are present - done to ensure motors are shutdown if program closes
    public void ShutDownAllDO() throws PhidgetException {
        if (ifk.isAttached()) {
            if (ifk.getOutputCount() > 0) {
                for (int j = 0; j < ifk.getOutputCount(); j++) {
                    // System.out.println("Shutting down output: "+j);
                    ifk.setOutputState(j, false);
                }
            }
        }
    }
    public void close() throws PhidgetException {
        ShutDownAllDO();
        ifk.close();
        ifk = null;
    }
//    public static void LogPhidgetsException(WWControlServerLogger WWCSlog, PhidgetException ex) {
//        String OutMessage = ex.getDescription() + "Phidget Error" + ex.getErrorNumber();
//        System.out.println(OutMessage);
//        WWCSlog.RecordLogEntry(OutMessage);
//    }
    public boolean isAttached() throws PhidgetException{     
            return ifk.isAttached();   
    }
    private void RecordAllDIState(ArrayList<AbstractDataRecorder> DataRecList) throws PhidgetException {
        for (int k = 0; k < ifk.getInputCount(); k++) {
            for (AbstractDataRecorder DataRec : DataRecList) {
                DataRec.InsertDIRecord(ifkSerial, k, ifk.getInputState(k));
            }           
        }
    }
    private void RecordAllDOState(ArrayList<AbstractDataRecorder> DataRecList) throws PhidgetException {
        for (int k = 0; k < ifk.getOutputCount(); k++) {
            for (AbstractDataRecorder DataRec : DataRecList) {
                DataRec.InsertDORecord(ifkSerial, k, ifk.getOutputState(k));
            }
        }
    }
    public abstract void RecordIFKState(WWControlServerLogger WWCSlog,ArrayList<AbstractDataRecorder> DataRecList) throws PhidgetException;
    public abstract void AddOutputChangeListener(OutputChangeListener OL);
    public abstract void AddInputChangeListener(InputChangeListener IL);
    public abstract void AddSensorChangeListener(SensorChangeListener SL);
    public abstract void ExecuteDefaultAttach() throws PhidgetException;
    public abstract void ExecuteDefaultDetach();
//    public GetSensorValue(SNUM,DYNR){
//        try{
//            
//        }
//    }
}
